﻿Ext.define('app.system.project_role_auth.Index',
{
    extend: 'Ext.grid.Panel',
    border: true,
    title: '项目角色权限',
    viewConfig: {
        enableTextSelection: true //可以复制单元格文字
    },
    multiColumnSort: true,
    forceFit: true,
    tbar: [],
    roleColumn: [],
    columns: [],
    initComponent: function () {
        var me = this;

        me.store = Ext.create("app.ux.PrmStore", { url: '/api/services/app/projectRole/GetRoleAuth' });

        App.AjaxGet('/api/services/app/projectRole/GetAll', function (data) {
            var _columns = [{ header: '项目权限', dataIndex: 'projectAuth' }];
            me.roleColumn = data.result;
            for (var i = 0; i < data.result.length; i++) {
                var record = data.result[i];
                _columns.push({ xtype: 'checkcolumn', sortable: false, header: record.roleName, dataIndex: Ext.util.Format.lowercase(record.roleName) });
            }
            me.reconfigure(me.store, _columns);
        });

        me.tbar = [
            { text: '保存', icon: '/Content/images/page_save.png', handler: function () { me.saveAuth(); } },
            "-",
            {
                text: '全选', icon: '/Content/images/accept.png', handler: function () {
                    var store = me.store;
                    for (var i = 0; i < store.getCount() ; i++) {
                        var record = store.getAt(i);
                        console.log(me.roleColumn);
                        for (var j = 0; j < me.roleColumn.length; j++) {
                            var c = me.roleColumn[j];
                            record.data[c.roleName] = true;
                        }
                        record.commit();
                    }
                }
            },
            "-",
            {
                text: '全不选', icon: '/Content/images/cancel.png', handler: function () {
                    var store = me.store;
                    for (var i = 0; i < store.getCount() ; i++) {
                        var record = store.getAt(i);
                        for (var j = 0; j < me.roleColumn.length; j++) {
                            var c = me.roleColumn[j];
                            record.data[c.roleName] = false;
                        }
                        record.commit();
                    }
                }
            }
        ];
        me.callParent(arguments);
    },
    saveAuth: function () {
        var me = this;
        var arrys = [];
        for (var i = 0; i < me.store.getCount() ; i++) {
            var _record = me.store.getAt(i);
            arrys.push(_record.data);
        }
        App.AjaxPost('/api/services/app/projectRole/SetRoleAuth', { records: arrys }, function () {
            me.store.load();
        });
    }
});
